﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Zj.HttpCodeDemo.Domain.Events;

namespace Zj.HttpCodeDemo.Bus.Simple
{
    public class MessageQueue
    {
        public readonly ConcurrentQueue<IEvent> _queue;
        public EventHandler<EventProcessedArgs> eventPushed;
        public MessageQueue()
        {
            this._queue = new ConcurrentQueue<IEvent>();
        }

        public void Push(IEvent @event)
        {
            _queue.Enqueue(@event);
            OnEventPushed(new EventProcessedArgs() { Event = @event });
        }

        public IEvent Pop()
        {
            var succeed = _queue.TryDequeue(out IEvent @event);
            return succeed ? @event : null;
        }
        private void OnEventPushed(EventProcessedArgs eventProcessedArgs)
        {
            this.eventPushed?.Invoke(this, eventProcessedArgs);
        }
    }
}
